﻿using d1Common;
using d1Common.WebController;
using d1Project.Modules.Common.do4A.Models;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Common.do4A.Controllers.a1
{
    /// <summary>
    /// 移动端注册用户
    /// </summary>
    [Route("do4A/a1/User2")]
    public class User2Controller : Authorize1Controller
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private do4ADbContext db = new do4ADbContext();

        [PermissionCheck(Id = "do4A$User2", Operation = 0)]
        public async Task<IHttpActionResult> Get([FromUri]User2GetVm model)
        {
            string fileAllPath = doServices.Instance.DoStorage.getAccessUrl("");
            var entities = db.User2s.AsQueryable();
            if (!string.IsNullOrEmpty(model.Code))
            {
                entities = entities.Where(t => t.Code == model.Code);
            }
            if (!string.IsNullOrEmpty(model.Name))
            {
                entities = entities.Where(t => t.Name.Contains(model.Name));
            }
            if (!string.IsNullOrEmpty(model.MobliePhone))
            {
                entities = entities.Where(t => t.MobilePhone == model.MobliePhone);
            }
            if (!string.IsNullOrEmpty(model.Email))
            {
                entities = entities.Where(t => t.Email == model.Email);
            }
            int count = await entities.CountAsync();

            var result = await entities.Select(t => new
            {
                t.Id,
                t.Code,
                t.Name,
                Icon = t.Icon,
                IconLong = fileAllPath + t.Icon,
                t.MobilePhone,
                t.Address,
                t.Sex,
                t.IsAvailable,
                t.Email,
                t.CreateById,
                t.CreateByName,
                t.CreateTime,
                t.UpdateTime
            }).OrderByDescending(t => t.CreateTime).ToPagedListAsync(model);
            return Ok(new { rows = result, total = count });
        }

        [PermissionCheck(Id = "do4A$User2", Operation = 0)]
        public async Task<IHttpActionResult> Get(string Id)
        {
            if (string.IsNullOrEmpty(Id)) this.Error("Id不能为空");
            string fileAllPath = doServices.Instance.DoStorage.getAccessUrl("");
            var entity = await db.User2s.Where(t => t.Id == Id)
                .Select(t => new
                {
                    t.Id,
                    t.Code,
                    t.Name,
                    IconLong = fileAllPath + t.Icon,
                    Icon = t.Icon,
                    t.MobilePhone,
                    t.Address,
                    t.BirthDay,
                    t.IdCard,
                    t.IsAvailable,
                    t.Email,
                    t.Sex,
                    t.CreateById,
                    t.CreateByName,
                    t.CreateTime,
                    t.UpdateById,
                    t.UpdateByName,
                    t.UpdateTime
                }).FirstOrDefaultAsync();
            if (entity == null) this.Error("用户不存在");
            return Ok(entity);
        }

    }

    public class User2GetVm : VmBase
    {
        /// <summary>
        /// code
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 真实姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 手机号
        /// </summary>
        public string MobliePhone { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; }
    }
}
